<?php
declare (strict_types = 1);

namespace app\middleware;

use app\model\InnerTestConfigModel;
use app\model\InnerTestUserModel;
use app\model\UserModel;

class InnerTest
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $isOpen = (new InnerTestConfigModel())->getInfo();

        if($isOpen['is_all_open'] == 1){

            //开启测试
            $token = !empty($header) && isset($header["token"]) ? $header["token"] : "";

            if(!isset($token) || empty($token)){
                return json(['code' => -2, 'msg' => '请先登录']);
            }

            $user = (new UserModel())->getUserCacheByOpenId($token);
            if(!$user){
                return json(['code' => -2, 'msg' => '登录已过期']);
            }

            //检测在不在测试白名单
            $isWhite = (new InnerTestUserModel())->isWhiteUser($user['Uid']);
            if(!$isWhite){
                return json(['code' => -2, 'msg' => '您不在测试白名单中']);
            }
        }
        return $next($request);
    }
}
